package game.actors;

import edu.monash.fit2099.engine.actions.Action;
import edu.monash.fit2099.engine.actions.ActionList;
import edu.monash.fit2099.engine.actions.DoNothingAction;
import edu.monash.fit2099.engine.actors.Actor;
import edu.monash.fit2099.engine.actors.attributes.BaseAttributes;
import edu.monash.fit2099.engine.displays.Display;
import edu.monash.fit2099.engine.displays.Menu;
import edu.monash.fit2099.engine.positions.GameMap;
import game.weapons.BareFist;

/**
 * Player/Explorer with Hydration & Warmth mechanics (REQ1).
 * - Starts at HP=100 (passed from Application/Earth)
 * - Hydration starts at 20 and decreases by 1 each non-sleep turn
 * - Warmth starts at 30 and decreases by 1 each non-sleep turn
 * - If either Hydration or Warmth reaches 0, the player collapses (game over)
 * - Bedroll sleep (multi-turn) pauses Hydration/Warmth decay (handled via multi-turn action flow)
 */
public class Player extends Actor {

    private final int maxHydration = 20;
    private final int maxWarmth = 30;
    private int hydration = maxHydration;
    private int warmth = maxWarmth;

    public Player(String name, char displayChar, int hitPoints) {
        super(name, displayChar, hitPoints);
        // intrinsic weapon (bare fist) already exists in this project
        this.setIntrinsicWeapon(new BareFist());
    }

    public int getHydration() { return hydration; }
    public int getMaxHydration() { return maxHydration; }
    public int getWarmth() { return warmth; }
    public int getMaxWarmth() { return maxWarmth; }

    public void addHydration(int delta) {
        hydration += delta;
        if (hydration > maxHydration) hydration = maxHydration;
        if (hydration < 0) hydration = 0;
    }

    public void addWarmth(int delta) {
        warmth += delta;
        if (warmth > maxWarmth) warmth = maxWarmth;
        if (warmth < 0) warmth = 0;
    }

    @Override
    public Action playTurn(ActionList actions, Action lastAction, GameMap map, Display display) {
        // If a multi-turn action is in progress (e.g., SleepAction), continue it.
        // While this path is taken, hydration/warmth DO NOT decay.
        if (lastAction != null && lastAction.getNextAction() != null) {
            return lastAction.getNextAction();
        }

        // Game over checks
        if (hydration <= 0 || warmth <= 0) {
            display.println(this + " has become unconscious due to dehydration/cold. Game Over.");
            map.removeActor(this);
            return new DoNothingAction();
        }

        // Show current status each turn
        display.println(
                this + " | 血量: "
                        + this.getAttribute(BaseAttributes.HEALTH) + "/"
                        + this.getMaximumAttribute(BaseAttributes.HEALTH)
                        + "  水分: " + hydration + "/" + maxHydration
                        + "  温度: " + warmth + "/" + maxWarmth
        );

        // Per-turn decay when not sleeping
        addHydration(-1);
        addWarmth(-1);

        // Note:
        // 引擎会在 World.prepareAllowableActions(...) 中自动把：
        // - 物品随身动作 (item.allowableActions(owner,map))
        // - 地上物品动作 (item.allowableActions(location))
        // - 地面/邻格交互 等，注入到 actions 里
        // 所以这里不需要再手动添加物品动作，直接出菜单即可。

        Menu menu = new Menu(actions);
        return menu.showMenu(this, display);
    }
}
